package com.iknow.activity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.AsyncTask.Status;
import android.os.Bundle;
import android.os.IBinder;
import android.text.Editable;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.iknow.IKnow;
import com.iknow.IKnowApiV4;
import com.iknow.User;
import com.iknow.data.Comment;
import com.iknow.data.QingBo;
import com.iknow.db.IKnowDataBase;
import com.iknow.task.GenericTask;
import com.iknow.task.TaskAdapter;
import com.iknow.task.TaskListener;
import com.iknow.task.TaskParams;
import com.iknow.task.TaskResult;
import com.iknow.ui.model.CommentListAdapter;
import com.iknow.util.StringUtil;
import com.iknow.view.widget.MyListView;
import com.iknow.xmpp.service.Friend;
import com.iknow.xmpp.service.aidl.IXmppConnection;
import com.iknow.xmpp.service.aidl.IXmppFacade;
import com.iknow.xmpp.service.aidl.IXmppFacade.Stub;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class CommentActivity extends Activity
{
  private static final Intent SERVICE_INTENT = new Intent();
  private final int ACTION_GET_COMMENT;
  private final int ATION_SUBMIT_COMMENT;
  private View.OnClickListener BackButtonClickListener;
  private View.OnClickListener SubmitButtonClickListener;
  private final String TAG = "CommentActivity";
  private AdapterView.OnItemClickListener listItemClickListener;
  private Button mActionButton;
  private int mActionCode;
  private CommentListAdapter mAdapter;
  private IKnow mApp;
  private Button mBackButton;
  private boolean mBinded;
  private EditText mDataEditText;
  private SimpleDateFormat mDateFormatTime;
  private MyListView mListView;
  protected TaskListener mListener;
  private ProgressDialog mProgressDialog;
  private QingBo mQingbo;
  private Button mSubmitButton;
  private ServiceConnection mSvrConn;
  private CommentTask mTask;
  private TextView mTitleText;
  private User mUser;
  private IXmppFacade mXmppFacade;

  static
  {
    SERVICE_INTENT.setComponent(new ComponentName("com.iknow", "com.iknow.xmpp.service.IKnowXmppService"));
  }

  public CommentActivity()
  {
    this.mSvrConn = new XmppServiceConnection(this, null);
    this.ACTION_GET_COMMENT = 0;
    this.ATION_SUBMIT_COMMENT = 1;
    this.mDateFormatTime = new SimpleDateFormat("MM-dd HH:mm");
    this.BackButtonClickListener = new View.OnClickListener(this)
    {
      public void onClick()
      {
        this.this$0.finish();
      }
    };
    this.SubmitButtonClickListener = new View.OnClickListener(this)
    {
      public void onClick()
      {
        CommentActivity.access$0(this.this$0);
      }
    };
    this.listItemClickListener = new AdapterView.OnItemClickListener(this)
    {
      public void onItemClick(, View paramView, int paramInt, long paramLong)
      {
        if (CommentActivity.access$1(this.this$0) == null);
        while (true)
        {
          Comment localComment;
          do
          {
            return;
            localComment = CommentActivity.access$2(this.this$0).getItem(paramInt);
          }
          while (localComment.getUid() == null);
          String str = localComment.getUid() + "@" + "imiknow.com";
          Intent localIntent = new Intent(this.this$0, FriendActivity.class);
          localIntent.putExtra("friendID", str);
          Friend localFriend = IKnow.mIKnowDataBase.getFriendInfo(str);
          if (localFriend != null)
            localIntent.putExtra("friend", localFriend);
          this.this$0.startActivity(localIntent);
        }
      }
    };
    this.mListener = new TaskAdapter(this)
    {
      public String getName()
      {
        return null;
      }

      public void onCancelled()
      {
      }

      public void onPostExecute(, TaskResult paramTaskResult)
      {
        if (CommentActivity.access$3(this.this$0) == 1)
          if (paramTaskResult == TaskResult.OK)
          {
            Toast.makeText(this.this$0, "发表成功", 0).show();
            CommentActivity.access$5(this.this$0).setText(null);
          }
        while (true)
        {
          do
            while (true)
            {
              CommentActivity.access$2(this.this$0).notifyDataSetChanged();
              if (CommentActivity.access$7(this.this$0) != null)
                CommentActivity.access$7(this.this$0).dismiss();
              return;
              Toast.makeText(this.this$0, CommentActivity.access$6(this.this$0).getErrorMessage(), 0).show();
            }
          while ((CommentActivity.access$3(this.this$0) != 0) || (paramTaskResult == TaskResult.OK));
          Toast.makeText(this.this$0, CommentActivity.access$6(this.this$0).getErrorMessage(), 0).show();
        }
      }

      public void onPreExecute()
      {
        if (CommentActivity.access$3(this.this$0) == 1)
          CommentActivity.access$4(this.this$0, "正在发表评论，请稍后");
        while (true)
        {
          do
            return;
          while (CommentActivity.access$3(this.this$0) != 0);
          CommentActivity.access$4(this.this$0, "正在获取评论数据，请稍后");
        }
      }

      public void onProgressUpdate(, Object paramObject)
      {
        if (CommentActivity.access$3(this.this$0) == 0)
        {
          List localList = (List)paramObject;
          CommentActivity.access$2(this.this$0).addComments(localList);
        }
        while (true)
        {
          do
            return;
          while (CommentActivity.access$3(this.this$0) != 1);
          Comment localComment = (Comment)paramObject;
          CommentActivity.access$2(this.this$0).addComment(localComment);
        }
      }
    };
  }

  private void getComments()
  {
    if ((this.mTask != null) && (this.mTask.getStatus() == AsyncTask.Status.RUNNING));
    while (true)
    {
      return;
      this.mActionCode = 0;
      this.mTask = new CommentTask(this, null);
      TaskParams localTaskParams = new TaskParams();
      localTaskParams.put("code", Integer.valueOf(0));
      this.mTask.setListener(this.mListener);
      CommentTask localCommentTask = this.mTask;
      TaskParams[] arrayOfTaskParams = new TaskParams[1];
      arrayOfTaskParams[0] = localTaskParams;
      localCommentTask.execute(arrayOfTaskParams);
    }
  }

  private void initView()
  {
    setContentView(2130903047);
    this.mListView = ((MyListView)findViewById(2131296292));
    this.mListView.setOnItemClickListener(this.listItemClickListener);
    this.mListView.setAdapter(this.mAdapter);
    this.mBackButton = ((Button)findViewById(2131296342));
    this.mBackButton.setOnClickListener(this.BackButtonClickListener);
    this.mActionButton = ((Button)findViewById(2131296344));
    this.mActionButton.setVisibility(8);
    this.mSubmitButton = ((Button)findViewById(2131296295));
    this.mSubmitButton.setOnClickListener(this.SubmitButtonClickListener);
    this.mDataEditText = ((EditText)findViewById(2131296294));
  }

  private void sendComment()
  {
    String str = this.mDataEditText.getText().toString();
    if (StringUtil.isEmpty(str))
      Toast.makeText(this, "请输入评论内容", 0).show();
    while (true)
    {
      do
        return;
      while ((this.mTask != null) && (this.mTask.getStatus() == AsyncTask.Status.RUNNING));
      this.mActionCode = 1;
      this.mTask = new CommentTask(this, null);
      TaskParams localTaskParams = new TaskParams();
      localTaskParams.put("code", Integer.valueOf(1));
      localTaskParams.put("data", str);
      this.mTask.setListener(this.mListener);
      CommentTask localCommentTask = this.mTask;
      TaskParams[] arrayOfTaskParams = new TaskParams[1];
      arrayOfTaskParams[0] = localTaskParams;
      localCommentTask.execute(arrayOfTaskParams);
    }
  }

  private void showProgress(String paramString)
  {
    this.mProgressDialog = new ProgressDialog(this);
    this.mProgressDialog.setCancelable(true);
    this.mProgressDialog.setMessage(paramString);
    this.mProgressDialog.show();
  }

  protected void onCreate(Bundle paramBundle)
  {
    super.onCreate(paramBundle);
    requestWindowFeature(1);
    this.mQingbo = ((QingBo)getIntent().getParcelableExtra("qingbo"));
    this.mUser = IKnow.mIKnowDataBase.getUser();
    this.mAdapter = new CommentListAdapter(getLayoutInflater());
    this.mApp = ((IKnow)getApplication());
    initView();
    getComments();
  }

  protected void onDestroy()
  {
    if ((this.mTask != null) && (this.mTask.getStatus() == AsyncTask.Status.RUNNING))
      this.mTask.cancel(true);
    super.onDestroy();
  }

  protected void onPause()
  {
    if (this.mBinded)
    {
      unbindService(this.mSvrConn);
      this.mBinded = false;
    }
    super.onPause();
  }

  protected void onResume()
  {
    if (!(this.mBinded))
      this.mBinded = bindService(SERVICE_INTENT, this.mSvrConn, 1);
    super.onResume();
  }

  private class CommentTask extends GenericTask
  {
    private String mErrorMessage;

    protected TaskResult _doInBackground()
    {
      TaskResult localTaskResult;
      TaskParams localTaskParams = paramArrayOfTaskParams[0];
      try
      {
        if (CommentActivity.access$3(this.this$0) == 0)
        {
          List localList = IKnow.mApi.getComments(CommentActivity.access$8(this.this$0).getID(), 0, 900);
          Object[] arrayOfObject2 = new Object[1];
          arrayOfObject2[0] = localList;
          publishProgress(arrayOfObject2);
          localTaskResult = TaskResult.OK;
        }
        else if (CommentActivity.access$3(this.this$0) == 1)
        {
          if ((CommentActivity.access$9(this.this$0).isConnected()) && (((CommentActivity.access$10(this.this$0) == null) || (CommentActivity.access$10(this.this$0).createConnection() == null) || (!(CommentActivity.access$10(this.this$0).createConnection().authenticatContentUser())))))
          {
            this.mErrorMessage = "网络超时，稍候再试";
            localTaskResult = TaskResult.FAILED;
            break label316:
          }
          String str = localTaskParams.getString("data");
          IKnow.mApi.comment(CommentActivity.access$8(this.this$0).getID(), str);
          if (CommentActivity.access$1(this.this$0) != null)
          {
            localComment = new Comment("", CommentActivity.access$1(this.this$0).getUID(), CommentActivity.access$1(this.this$0).getNick(), CommentActivity.access$11(this.this$0).format(new Date(System.currentTimeMillis())), str);
            Object[] arrayOfObject1 = new Object[1];
            arrayOfObject1[0] = localComment;
            publishProgress(arrayOfObject1);
            localTaskResult = TaskResult.OK;
            break label316:
          }
          Comment localComment = new Comment("", "", "网友", CommentActivity.access$11(this.this$0).format(new Date(System.currentTimeMillis())), str);
        }
      }
      catch (Exception localException)
      {
        this.mErrorMessage = "网络超时，稍后再试";
        localException.printStackTrace();
        localTaskResult = TaskResult.FAILED;
        label316: return localTaskResult;
      }
    }

    public String getErrorMessage()
    {
      return this.mErrorMessage;
    }
  }

  private class XmppServiceConnection
  implements ServiceConnection
  {
    public void onServiceConnected(, IBinder paramIBinder)
    {
      CommentActivity.access$12(this.this$0, IXmppFacade.Stub.asInterface(paramIBinder));
    }

    public void onServiceDisconnected()
    {
      CommentActivity.access$12(this.this$0, null);
    }
  }
}